package top.kuxiao.crmserver.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import top.kuxiao.crmserver.pojo.Customer;
import top.kuxiao.crmserver.pojo.CustomerLoss;
import top.kuxiao.crmserver.pojo.Orders;
import top.kuxiao.crmserver.service.ICustomerLossService;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.service.IOrdersService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定时器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-18
 */
@Configuration
@CrossOrigin
@EnableScheduling
public class Timer {

    @Autowired
    ICustomerService customerService;

    @Autowired
    IOrdersService ordersService;

    @Autowired
    ICustomerLossService customerLossService;

    /**
     * 定时任务，每天凌晨两点执行
     * */
    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "*/5 * * * * ?")
    public void start(){

        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();

        //DATE_FORMAT(orders_date,'%Y')
        ordersQueryWrapper.select("max(UNIX_TIMESTAMP(orders_date)) as last,customer_id").groupBy("customer_id");

        //查询全部订单
        List<Map<String,Object>>maps=ordersService.listMaps(ordersQueryWrapper);

        System.out.println("++++++++"+maps+"++++++++");

        //查询全部流失
        List<CustomerLoss> losses = customerLossService.list();


        Date date = new Date();
        long now = date.getTime();




        boolean flag;


//        for (Orders orders : list) {
        for (Map<String, Object> orders : maps) {
            flag=true;
            long before = (long) orders.get("last")*1000;
            if ((now-before)/(1000*60*60*24)>=180){

                for (CustomerLoss loss : losses) {
                    //TODO 存在就不创建,需要修改
                    Integer id= (Integer) orders.get("customer_id");
                    if ( id.equals(loss.getCustomerId())){
                        flag=false;
                        break;
                    }

                }
                if (flag){
                    CustomerLoss customerLoss = new CustomerLoss();

                    Customer byId = customerService.getById((Integer)orders.get("customer_id"));


                    customerLoss.setLastOrderTime(new Date((Long) orders.get("last")*1000));
                    customerLoss.setCustomerId(byId.getCustomerId());
                    customerLoss.setCustomerLossState("预警");
                    customerLoss.setAccountManagerId(byId.getCustomerCid());
                    customerLossService.save(customerLoss);
                }

            }

        }
    }
}
